
package com.rx.mgt.controller;  

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.rx.mgt.domain.AdminUser;
import com.rx.mgt.domain.UserAlias;
import com.rx.mgt.domain.UserInfoExt;
import com.rx.mgt.po.UsersBo;
import com.rx.mgt.service.DomainService;
import com.rx.mgt.service.SysLogService;
import com.rx.mgt.service.UserAliasService;
import com.rx.mgt.util.IpUtil;

/** 
 * ClassName:UserAliasController 
 * Function :
 * Reason   : 
 * Date     :Jan 31, 2017 9:35:20 PM 
 * @author   luolei 
 * @since    JDK 1.6
 */
@Controller
public class UserAliasController {
    @Autowired
    UserAliasService userAliasService;
    @Autowired
    DomainService domainService;
    @Autowired
    SysLogService sysLogService;
    
    @RequestMapping(value="/users-alias-list.htmlx")
    public String  usersAliasList(ModelMap model,HttpServletRequest request ,UsersBo usersBo){
        AdminUser user = (AdminUser) request.getSession().getAttribute("user");
        if("2".equals(user.getType())){
            usersBo.setDomain(user.getDomain());
        }
        List list = userAliasService.getAllUserAlias(usersBo);
        model.addAttribute("list",list);
        model.addAttribute("usersBo",usersBo);
        System.out.println("111111111111111");
        return "user/users-alias-list";
    }
    @SuppressWarnings({ "rawtypes", "unchecked" })
    @RequestMapping(value="/users-alias-add.htmlx")
    public String  add(ModelMap model,HttpServletRequest request,UserAlias userAlias,UsersBo usersBo){
        AdminUser user = (AdminUser) request.getSession().getAttribute("user");
        if("2".equals(user.getType())){
            usersBo.setDomain(user.getDomain());
        }
        List list = domainService.getDomainInfoExt(usersBo);
        model.addAttribute("domainList",list);
        Map map =new HashMap();
        map.put("result", "success");
        if(userAlias.getId()==null||userAlias.getId()<1){
            model.addAttribute("userAlias",userAlias);
            model.addAttribute("method","add");
            return "user/users-alias-add";
        }
        UserAlias info= userAliasService.get(userAlias.getId());
        if(info!=null){
            model.addAttribute("userAlias",info);
        }
        return "user/users-alias-add";
    }
    @SuppressWarnings({ "rawtypes", "unchecked" })
    @RequestMapping(value="/userAliasSave.htmlx")
    @ResponseBody
    public void  userAliasSave(ModelMap model,HttpServletRequest request , HttpServletResponse response,UserAlias userAlias) throws IOException{
        AdminUser user = (AdminUser) request.getSession().getAttribute("user");
        sysLogService.add("更新别名", IpUtil.getIpAddr(request), "管理", user.getId()+"", user.getUser_name());
        response.setContentType("text/html;charset=utf-8");
        Map map =new HashMap();
        userAliasService.changelUserAlias(userAlias);
        response.getWriter().print("{\"errMsg\":\"操作成功\",\"result\":\"success\"}");
    }
    @SuppressWarnings({ "rawtypes", "unchecked" })
    @RequestMapping(value="/deleteUserAlias.htmlx")
    @ResponseBody
    public Map  deleteUserAlias(HttpServletRequest request ,String ids){
        AdminUser user = (AdminUser) request.getSession().getAttribute("user");
        sysLogService.add("删除别名", IpUtil.getIpAddr(request), "管理", user.getId()+"", user.getUser_name());
        Map map =new HashMap();
        if(ids!=null){
            String[] id=ids.split(",");
            for(String s:id){
                userAliasService.delete(Long.valueOf(s));
            }
        }
        map.put("result", "success");
        System.out.println("111111111111111");
        return map;
    }
    
    @SuppressWarnings({ "rawtypes", "unchecked" })
    @RequestMapping(value="/stopUseAliasr.htmlx")
    @ResponseBody
    public Map  stopUserAlias(HttpServletRequest request ,UserAlias userAlias){
        AdminUser user = (AdminUser) request.getSession().getAttribute("user");
        sysLogService.add("停止别名", IpUtil.getIpAddr(request), "管理", user.getId()+"", user.getUser_name());
        userAliasService.stopUserAlias(userAlias);
        Map map =new HashMap();
        map.put("result", "success");
        return map;
    }
    @SuppressWarnings({ "rawtypes", "unchecked" })
    @RequestMapping(value="/startUserAlias.htmlx")
    @ResponseBody
    public Map  startUserAlias(HttpServletRequest request ,UserAlias userAlias){
        AdminUser user = (AdminUser) request.getSession().getAttribute("user");
        sysLogService.add("开启别名", IpUtil.getIpAddr(request), "管理", user.getId()+"", user.getUser_name());
        userAliasService.startUserAlias(userAlias);
        Map map =new HashMap();
        map.put("result", "success");
        return map;
    }
    
}
  